<?php

include('../../resources/LogSiteSearch.php');
require('../db_config/Config.inc.php');
require('../db_config/DbConnect.class.php');
require('vo/Site.php');
require('vo/Area.php');
require('vo/LogInfo.php');
require('vo/Contact.php');


class VFC {
	
	var $db;
	
	function VFC() {
		$this->connect();
	}	
	//connect to db
	function connect()
	{
		//Set up all paramaters for the connection
		$this->db = new DbConnect();
		  
		//Open the connection to the database
		$this->db->open() or die($db->error());
	}
	
	/**
	 *
	 * @param unknown_type $zipCode
	 * @param unknown_type $serviceTypeID
	 * @param LogInfo $logInfo
	 * @return unknown
	 */
	//search for sites by zip and service type
	function getSitesByZipAndService( $zipCode, $serviceTypeID, $logInfo = null )
	{
	
		//query db (do not list flagged sites)
		//TO-DO find all sites that can be listed and service given zip with selected service. 
		$query = "SELECT * FROM SiteService LEFT JOIN Site ON SiteService.SiteID = Site.SiteID LEFT JOIN Area ON Site.AreaID = Area.AreaID WHERE SiteServiceID IN (SELECT SiteServiceID From SiteServiceZip where Zip = '" . $zipCode . "') AND ServiceTypeID = '" . $serviceTypeID . "' AND  SiteParticipationFlag = 1";
		
		$result = mysql_query( $query ) or die(mysql_error());
		
		if (!$result)
			return null;
		
		$return = array();
		while ($row = mysql_fetch_assoc($result)) {
			$site = new Site();
			$site->setRowData($row);
			$site->area = new Area();
			$site->area->setRowData($row);
			$return[] = $site; // push it on the array
		}
		
		$numberOfResults = count( $return );
		logSearch(  $zipCode, $numberOfResults, $logInfo = date("Y-m-j G:i:s T"));
	
		return $return;
	}
	
	//list all food bank service types with ids
	function getFBServiceTypes()
	{
		 //query db
		$tableName = "SiteService";
		$query = "SELECT * FROM `" . $tableName . "`";
		$result = mysql_query( $query ) or die(mysql_error());
		
		return $result;
	}
	
	//get details
	function getSiteDetails( $siteId )
	{
	
		//query db for site details
		$tableName = "Site";
		$columnName = "SiteID";
		$query = "SELECT * FROM `" . $tableName . "` WHERE `" . $columnName . "` = '" . $siteId . "'";
	
		$result = mysql_query( $query ) or die(mysql_error());
	

		return $result;
	}
	
	//Make an entry in the log for the selected site item.
	function selectionLogEntry ($site)
	{
	}
}

?>